package login.Model.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class UserDao {
    /**
     * 判断用户名是否存在
     * @param name
     * @return
     * @throws Exception
     */
    public static Boolean exit(String name) throws Exception{
        Connection conn = DBUtil.getconnection();
        String sql = "select * from user where username = ? ";
        PreparedStatement pstmt = conn.prepareStatement(sql);
        pstmt.setString(1,name);
        ResultSet rs = pstmt.executeQuery();
        Boolean result = rs.next();
        rs.close();
        pstmt.close();
        return result;
    }

    /**
     * 判断用户名与密码对应关系
     * @param name
     * @param pwd
     * @return
     * @throws Exception
     */
    public static Boolean Judge(String name,String pwd) throws Exception{
        //获取连接
        Connection conn = DBUtil.getconnection();
        String sql = "select * from user where username = ? and password = ?";
        PreparedStatement pstmt = conn.prepareStatement(sql);
        pstmt.setString(1,name);
        pstmt.setString(2,pwd);
        ResultSet rs = pstmt.executeQuery();
        Boolean result = rs.next();
        rs.close();
        pstmt.close();
        return result;
    }

    /**
     * 找回用户名或密码信息验证
     * @param name
     * @param direction
     * @param phase
     * @param postbox
     * @param phoneNumber
     * @return
     * @throws Exception
     */

    public static Boolean judgeInformation(String name,String direction,String phase,String postbox,String phoneNumber) throws Exception {
        //获取连接
        Connection conn = DBUtil.getconnection();
        String sql = "select * from user where name = ? and direction = ? and phase = ? and postbox = ? and phoneNumber = ?";
        PreparedStatement pstmt = conn.prepareStatement(sql);
        pstmt.setString(1,name);
        pstmt.setString(2,direction);
        pstmt.setString(3,phase);
        pstmt.setString(4,postbox);
        pstmt.setString(5,phoneNumber);
        ResultSet rs = pstmt.executeQuery();
        Boolean result = rs.next();
        rs.close();
        pstmt.close();
        return result;
    }

    public static Boolean judgeMailbox(String name,String postbox) throws Exception {
        //获取连接
        Connection conn = DBUtil.getconnection();
        String sql = "select * from user where name = ? and postbox = ?";
        PreparedStatement pstmt = conn.prepareStatement(sql);
        pstmt.setString(1,name);
        pstmt.setString(2,postbox);
        ResultSet rs = pstmt.executeQuery();
        Boolean result = rs.next();
        rs.close();
        pstmt.close();
        return result;
    }

    public static String getInformation(String name)throws Exception{
        //获取连接
        Connection conn = DBUtil.getconnection();
        String sql = "select * from user where name = ? ";
        PreparedStatement pstmt = conn.prepareStatement(sql);
        pstmt.setString(1,name);
        ResultSet rs = pstmt.executeQuery();
        String username = null;
        String password = null;
        while (rs.next()) {
            username = rs.getString(2);
            password = rs.getString(3);
        }

        String information = "用户名为："+username+"\n"+"密码为："+password;
        rs.close();
        pstmt.close();
        return information;
    }


}

